<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Marks_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function get_by_id($mark_id) {
        return $this->db->get_where('marks', array('id' => $mark_id))->row();
    }

//    function get_by_user($user_id) {
  //      return $this->db->get_where('events', array('user_id' => $user_id))->result();
    //}

    function get_student_marks($student_id, $class_id) {
        $this->db->select('*')
                 ->from('marks m')
                 ->join('evals e', 'm.eval_id=e.id')
				 ->join('cours c', 'e.cours_id=c.id')
                 ->where('c.class_id', $class_id)
                 ->where('m.student_id', $student_id);

        return $this->db->get()->result();
    }
	
	function get_class_marks($class_id, $cours_id) {
        $this->db->select('*')
                 ->from('marks m')
                 ->join('evals e', 'm.eval_id=e.id')
				 ->join('cours c', 'e.cours_id=c.id')
                 ->where('c.class_id', $class_id)
                 ->where('m.student_id', $student_id);

        return $this->db->get()->result();
    }

    function get_by_user_for_class($user_id, $class_id) {
        $this->db->select('*')
                 ->from('classes_events ce')
                 ->where('e.user_id', $user_id)
                 ->where('ce.class_id', $class_id)
                 ->join('events e', 'e.id=ce.event_id');

        return $this->db->get()->result();
    }

    function create($title, $content) {
        $this->db->set('user_id', $this->tank_auth->get_user_id());
        $this->db->set('created', 'now()', false);
        $this->db->set('title', $title);
        $this->db->set('content', $content);
        $this->db->insert('events');
    }

    function update($event_id, $title, $content) {
        $data = array(
            'title' => $title,
            'content' => $content,
        );
        $this->db->update('events', $data, array('id' => $event_id));
    }

    function delete($event_id) {
        $this->db->delete('classes_events', array('event_id' => $event_id));
        $this->db->delete('events', array('id' => $event_id));
    }

    function link($class_id, $event_id) {
        $this->db->set('class_id', $class_id);
        $this->db->set('event_id', $event_id);
        $this->db->insert('classes_events');
    }

    function unlink($class_id, $event_id) {
        $this->db->delete('classes_events', array('class_id' => $class_id, 'event_id' => $event_id));
    }

    function show($class_id, $event_id) {
        $this->db->set('published', 'now()', false)
                 ->where('class_id', $class_id)
                 ->where('event_id', $event_id);

        $this->db->update('classes_events');
    }

    function mask($class_id, $event_id) {
        $this->db->set('published', null)
                 ->where('class_id', $class_id)
                 ->where('event_id', $event_id);

        $this->db->update('classes_events');
    }
}